package com.cmcc.pp.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.cmcc.pp.R;
import com.cmcc.pp.base.BActivity;
import com.cmcc.pp.ui.widget.RefreshLinearLayout;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.zrspring.libv2.util.RegexUtils;
import com.zrspring.libv2.util.ToastUtils;

import java.net.URI;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ABC on 2017/12/11.
 * 登陆界面
 */

public class LoginActivity extends BActivity {
    @Bind(R.id.ed_phone)
    EditText edPhone;
    @Bind(R.id.ed_img_code)
    EditText edImgCode;
    @Bind(R.id.bt_login)
    Button btLogin;
    @Bind(R.id.login_image_close)
    ImageView loginImageClose;
    @Bind(R.id.ed_password)
    EditText edPassword;
    @Bind(R.id.content)
    RefreshLinearLayout refreshLinearLayout;
    @Bind(R.id.draweeview_verifycode)
    SimpleDraweeView draweeviewVerifycode;


    private String UserName = "";
    private String PassWord = "";
    private String VerifyCode = "";

    private String strTips = "";//提示文字

    private Handler handler;
    private boolean flag;

    @Override
    protected boolean getHasTitle() {
        return true;
    }

    @Override
    public int getContentLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        getHeader().setTitle(R.string.login);
        getHeader().setLeftAndImageGone();

        flag = false;
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 1:
                        refreshLinearLayout.updateStatus(RefreshLinearLayout.Companion.getFAIL(), strTips, true);
                        break;
                    case 2:
                        refreshLinearLayout.updateStatus(RefreshLinearLayout.Companion.getSUCCESS());
                        break;
                    case 3:
                        if (flag) {
                            refreshLinearLayout.updateStatus(RefreshLinearLayout.Companion.getLOADING());
                            handler.sendEmptyMessageDelayed(2, 5000);
                        } else {
                            refreshLinearLayout.updateStatus(RefreshLinearLayout.Companion.getLOADING());
                            handler.sendEmptyMessageDelayed(1, 5000);
                        }
                        break;
                }
            }
        };

        refreshLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                refreshLinearLayout.packUpHeader();
            }
        });

        inintViews();
    }

    @Override
    public void loadData() {

    }

    private void inintViews() {
//        codeUtils = CodeUtils.getInstance();
//        Bitmap bitmap = codeUtils.createBitmap("qfghd");
//        imageCode.setImageBitmap(bitmap);
//https://gss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/eac4b74543a98226088e26648a82b9014a90eb22.jpg
        Uri imageUri = Uri.parse("https://gss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhypic/item/eac4b74543a98226088e26648a82b9014a90eb22.jpg");
        draweeviewVerifycode.setImageURI(imageUri);

        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setUri("imageUri")
                .setTapToRetryEnabled(true)
                .setOldController(draweeviewVerifycode.getController())
                .build();
        draweeviewVerifycode.setController(controller);
    }


    @OnClick({
            R.id.bt_login,
            R.id.login_image_close
    })
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_login:
//                if (IsValid()) {
//                    //在输入了账号密码后执行验证码验证接口
//                    if (IsRightCode()) {
//                        login();
//                    }
//                }
                login();
                break;

            case R.id.login_image_close:
                UserName = "";
                edPhone.setText(UserName);
                break;
        }
    }

    //登陆
    private void login() {
        Intent intent = new Intent(this, MianActivityDrawerLayout.class);
        startActivity(intent);
        finish();
    }


    //判断手机号、密码是否输入
    private boolean IsValid() {
        boolean result = true;

        UserName = edPhone.getText().toString().trim();
        PassWord = edPassword.getText().toString().trim();
        VerifyCode = edImgCode.getText().toString().trim();
        Log.e("codeStr", VerifyCode);

        if (TextUtils.isEmpty(UserName)) {
            ToastUtils.show("请输入手机号码");
            result = false;
        } else if (!RegexUtils.isMobile(UserName)) {
            ToastUtils.show("请输入正确的手机号码");
            result = false;
        } else if (TextUtils.isEmpty(PassWord)) {
            ToastUtils.show("请输入登陆密码");
            result = false;
        } else if (TextUtils.isEmpty(VerifyCode)) {
            strTips = "请输入图形验证码";
            Message message = Message.obtain();
            message.what = 1;
            handler.sendMessage(message);
            result = false;
        }

        return result;
    }

    //验证验证码是否正确
    private boolean IsRightCode() {
        boolean isrightcode = true;

        //这里执行接口

        return isrightcode;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


}
